﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace APPLICATION.UserManager
{
    public partial class FrmAddEditAction : Form
    {
        public FrmAddEditAction()
        {
            InitializeComponent();
        }
        public bool editData;
        public string CodeID;
        public object mgrid;
        private void addNewAction()
        {
            editData = false;         
            bindingSourceAction.AddNew();
        }

        private bool check()
        {
            if (string.IsNullOrEmpty(textBoxActionName.Text))
            {
                MessageBox.Show(this, "Ведите наименование", "User manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            return false;        
        }

        private bool save()
        {
            if (check())
            {return false;}

            UMdataAccess dataAcess = new UMdataAccess();
            SqlParameter[] sqlPars=new SqlParameter[4];

            sqlPars[0] = new SqlParameter("@Action", SqlDbType.Int);
            sqlPars[1] = new SqlParameter("@CodeID", SqlDbType.Int);
            sqlPars[1].Direction = ParameterDirection.InputOutput;
            if (editData)
            { 
                sqlPars[0].Value = 1;
                sqlPars[1].Value = CodeID; 
            }
            else
            {
                sqlPars[0].Value = 0;
                sqlPars[1].Value = DBNull.Value; 
            }

            sqlPars[2] = new SqlParameter("@hName", SqlDbType.NVarChar, 200);
            sqlPars[2].Value = textBoxActionName.Text;

            sqlPars[3] = new SqlParameter("@mgrid", SqlDbType.Int);
            if (chekBox1.Checked)
            { sqlPars[3].Value = DBNull.Value;  }
            else
            { sqlPars[3].Value = mgrid;    }
            

            return dataAcess.addOrUpdateAction(sqlPars,bindingSourceAction);
        }

        private void GetParentNodeText()
        {

            if (editData || mgrid==null)
            {
                chekBox1.Visible = false;
                labelParentNodeText.Visible = false;
                return;
            }
            else
            {
                chekBox1.Visible = true;
                labelParentNodeText.Visible = true;
            }

            if(chekBox1.Checked)
            {
            labelParentNodeText.Visible = false;
            bindingSourceActionMeta.Filter = "CodeID=" + mgrid.ToString();
            }
            else
            {
                labelParentNodeText.Visible = true;
            }
        }

        private void FrmAddEditAction_Load(object sender, EventArgs e)
        {
            DataView dw = new DataView(GlobalDataFile.UserManagerDataSet.stActions);
            bindingSourceAction.DataSource = dw;

            textBoxActionName.DataBindings.Add("text", bindingSourceAction, "hName");

            DataView dwMeta = new DataView(GlobalDataFile.UserManagerDataSet.stActions);
            bindingSourceActionMeta.DataSource = dwMeta;
            labelParentNodeText.DataBindings.Add("Text", bindingSourceActionMeta, "hName");

            this.GetParentNodeText();
            if (editData)
            {
                bindingSourceAction.Filter = "CodeID=" + CodeID;                
            }
            else
            {
                this.addNewAction();
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.save())
            { this.Close(); }
        }

        private void BtnSaveAndNew_Click(object sender, EventArgs e)
        {
            if (this.save())
            {this.addNewAction(); }
            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();            
        }

        private void FrmAddEditAction_FormClosing(object sender, FormClosingEventArgs e)
        {
            bindingSourceAction.CancelEdit();
        }

        private void chekBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.GetParentNodeText();
        }
    }
}
